using System;
using OneDayModel.Interfaces;
using OneDayModel.Model;
using OneDayModel.PresentationLogic;
using OneDayModel.Tests.Mocks;

namespace OneDayModel.Tests.Fits.UIUsers
{
    public class CreateAlbumUser : ICreateAlbumUI
    {
        private readonly SessionContext sessionContext;
        private string albumNameToCreate;
        private IAlbum shownAlbum;
        private string shownError = string.Empty;
        private readonly IRepository repository = null;

        public CreateAlbumUser()
        {
            this.repository = new MockRepository();
            this.sessionContext = new SessionContext();

            new CreateAlbumUILogic(this, new AlbumsWorksModel(repository, null, sessionContext));
        }

        public event EventHandler<CreateAlbumArgs> onCreateAlbum;

        public void SetAlbumName(string albumName)
        {
            albumNameToCreate = albumName;
        }

        public void CreateAlbum()
        {
            shownError = string.Empty;
            shownAlbum = null;
            if (onCreateAlbum != null)
                onCreateAlbum(this, new CreateAlbumArgs(albumNameToCreate));

        }

        public void ShowAlbum(IAlbum album)
        {
            shownAlbum = album;
        }

        public void ShowAlbumNameCantBeEmptyError()
        {
            shownError = "Album Name Can't Be Empty";
        }

        public void ShowAlbumAlreadyExistError(string albumName)
        {
            shownError = string.Format("Album '{0}' already exist", albumName);
        }

        public string GetShowAlbumName()
        {
            return shownAlbum.Name;
        }

        public string GetShownError()
        {
            return shownError;
        }

        public void SetSessionContextUser(string alias)
        {
            IUser owner = repository.GetUserByAlias(alias);

            sessionContext.User = owner;

            ((IOnlineCache)repository).RegisterUserOnline(alias);
        }
    }
}